package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.CommResponseBean;
import com.shenzhen.horselive.beans.LockCityBean;
import com.shenzhen.horselive.beans.UserAcountBean;
import com.shenzhen.horselive.databinding.ActivityContinueLockCityBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.presenter.ViewObserver;
import com.shenzhen.horselive.ui.dialog.InputDialog;
import com.shenzhen.horselive.utils.DialogManager;

import java.text.NumberFormat;

public class ContinueLockCityActivity extends AppBarActivity {

    ActivityContinueLockCityBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding  =ActivityContinueLockCityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleDark();
        setTitle("继续锁城");
        initViews();
        initData();
    }

    private void initViews() {
        binding.btGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogManager.getInstance().showInputDialog(getContext()
                        , "请输入支付密码", "", new InputDialog.OnInputFinsh() {
                            @Override
                            public void onFinish(String s) {
                               pay(s);
                            }
                        });
            }
        });
    }

    private void pay(String s) {
        if (bean==null)
            return;
        ApiFactory.getApiService().lock(User.get().getMyInfo().getToken()
        ,bean.getData().getCityMasterId(),bean.getData().getMarginAmount(),s)
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<CommResponseBean>(this) {
                    @Override
                    protected void onNextInActive(CommResponseBean commResponseBean) {
                        if (commResponseBean.isSuccess())
                        {
                            showToast("锁城成功");
                            finish();

                        }else
                        {
                            showToast(commResponseBean.getErrMessage());
                        }
                    }
                });


    }

    LockCityBean bean;
    private void initData() {
        String  id =  getIntent().getStringExtra("id");
        if (id==null)
            return;

        ApiFactory.getApiService().lockCity(User.get().getMyInfo().getToken()
        ,id).compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<LockCityBean>(this) {
                    @Override
                    protected void onNextInActive(LockCityBean lockCityBean) {
                        if (lockCityBean.isSuccess())
                        {
                            bean = lockCityBean;
                            binding.tvNotice.setText("（需缴纳保证金为当前城市价值的"+lockCityBean.getData().getMarginRatio()+"%）");
                            binding.tvMoney.setText(lockCityBean.getData().getMarginAmount()+"蜂值");
                        }else
                        {
                            showToast(lockCityBean.getErrMessage());
                        }
                    }
                });

        //获取峰值的余额
        getAcountForType(1);
    }



    //帐户类型: 0:人民币帐户 ,1:蜂值帐户 ,2:红包股帐户 ,3:金卡帐户
    private void getAcountForType(int i) {
        ApiFactory.getApiService().accountTypes(User.get().getMyInfo().getToken(),i+"")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new ViewObserver<UserAcountBean>(this) {
                    @Override
                    protected void onNextInActive(UserAcountBean userAcountBean) {
                        if (userAcountBean.isSuccess())
                        {
                            NumberFormat numberFormat = NumberFormat.getNumberInstance();
                            numberFormat.setMaximumFractionDigits(10);//最多保留10位
                            double  value = Double.parseDouble(userAcountBean.getData().getValue());
//                           //显示个数
                            binding.tvLeftMoney.setText(String.format("峰值余额:%s(蜂值)",userAcountBean.getData().getAmount()));
                        }
                    }
                });

    }



    public static void start(Context context,String id) {
        Intent starter = new Intent(context, ContinueLockCityActivity.class);
        starter.putExtra("id",id);
        context.startActivity(starter);
    }


}